import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support import wait
from selenium.webdriver.support import expected_conditions as EC
from WebTest.WebAutomationEncapsulation.PublicToolsCategory.My_mysql import My_mysql
class UserUpdatePage:

    def __init__(self, driver):
        self.driver = driver

    """
        这里更新是根据userid更新如果使用username，需要进行数据库操作查找到user那么对应的id
    """

    def update_user_page(self,username,realname,phonenum,email):
        db = My_mysql()
        db.get_mysql(host='localhost', database='finance', user='root', password='root', port=3306, charset='utf8')
        ids = db.select_sql("select id from user where username = %s", (username))
        userid = ids[0][0]
        # 登陆完成后，进入到主页面，点击用户信息管理
        self.driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
        time.sleep(1)
        # 点击用户信息
        self.driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[1]/a').click()
        time.sleep(2)
        #点击对应用户的编辑
        # self.driver.find_element(By.XPATH,f'//button[@update-id="{userid}"]')
        #显式等待
        wait.WebDriverWait(self.driver,10).until(EC.presence_of_element_located((By.XPATH,f'//button[@update-id="{userid}"]'))).click()
        time.sleep(1)
        #输入真实姓名
        element_name = self.driver.find_element(By.XPATH,'//*[@id="realname_update_input"]')
        element_name.clear()
        element_name.send_keys(realname)
        time.sleep(1)
        #输入手机号
        element_phonenum = self.driver.find_element(By.XPATH,'//*[@id="phone_update_input"]')
        element_phonenum.clear()
        element_phonenum.send_keys(phonenum)
        time.sleep(1)
        #输入邮箱
        element_email = self.driver.find_element(By.XPATH,'//*[@id="email_update_input"]')
        element_email.clear()
        element_email.send_keys(email)
        time.sleep(1)
        #点击更新
        self.driver.find_element(By.XPATH,'//*[@id="user_update_btn"]').click()
        time.sleep(1)

        """
            获取更新的结果信息，成功的信息为 用户信息修改成功，实际上还要对修改后的进行格式验证但是我没写
        """
    def update_user_success_get_massage(self):
        #点击确认   不够稳定易出错
        return self.driver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button').text

    """
        真实姓名不合法
    """
    def update_user_realname_error_get_massage(self):
        #点击确认   不够稳定易出错
        return self.driver.find_element(By.XPATH,'//*[@id="userUpdateModal"]/div/div/div[2]/form/div[2]/div/span').text


    """
        手机号不合法
    """
    def update_user_phonenum_error_get_massage(self):
        #点击确认   不够稳定易出错
        return self.driver.find_element(By.XPATH,'//*[@id="userUpdateModal"]/div/div/div[2]/form/div[3]/div/span').text

    """
        邮箱不合法//*[@id="userUpdateModal"]/div/div/div[2]/form/div[4]/div/span
    """
    def update_user_email_error_get_massage(self):
        #点击确认   不够稳定易出错
        return self.driver.find_element(By.XPATH,'//*[@id="userUpdateModal"]/div/div/div[2]/form/div[4]/div/span').text
